<template>
  <div class="topbar">
    <div class="topbarin w">
      <!-- 左侧 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧 -->
      <ul class="right">
        <li>
          <img src="../assets/img/userImg.f8bbec5e.png" alt="" class="avatar" />
          用户名：游客
        </li>
        <li>我的积分：--</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogin">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>1</strong>
        </li>
        <li class="loginbtn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeiXinLoginAPI } from "@/request/api";
export default {
  computed: {
    ...mapState({
      isLogin: (state) => state.loginStatus.isLogin,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogin: "loginStatus/changeIsLogin",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
    }),
  },
  created() {
    // 等路由对象初始化完毕后再获取参数
    this.$router.onReady(async () => {
      console.log("***topbar-created", this.$route.query);
      let pathCode = this.$route.query.code;
      // 向后台发起微信扫码登录请求
      if (pathCode) {
        let res = await WeiXinLoginAPI({
          code: pathCode,
        });
        if (!res) return;
        console.log("aaa", res);
        // 根据res结果分情况判断
        if (res.code == 0) {
          // 1.提示扫码登录成功
          this.asyncShowToast({
            isShow: true,
            msg: "扫码登录成功",
            type: "success",
          });
          // 2.存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          // 3.登录后隐藏登录按钮显示购物车
          this.changeIsLogin(true);
          // 4.清除地址栏的code(避免重复发送微信扫码登录请求)
          this.$router.push(this.$route.path);
        } else if (res.code == 400) {
          // 二维码超时  code已经使用过  code错误
          this.asyncShowToast({
            isShow: true,
            msg: "二维码失效，请重新扫码",
            type: "error",
          });
          // 自动打开模态框
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          // 用户第一次使用微信扫码登录，并且没有绑定是手机号情况
          // 1.提示用户需要再绑定手机号码
          this.asyncShowToast({
            isShow: true,
            msg: "请使用手机号码再登录一次",
            type: "error",
          });
          // 2.自动打开模态框
          this.changeIsShowLoginModal(true);
          // 3.本地存储uuid给下次手机号码登录时使用
          localStorage.setItem("uuid", res.uuid);
          // 4.清除地址栏的code(避免重复发送微信扫码登录请求)
          this.$router.push(this.$route.path);
        }
      } else {
        console.log("进入了create的else部分");
        // 判断本地是否存在token，存在表示已登录，不存在就是未登录
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogin(Boolean(token));
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fff;
  .topbarin {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
      li {
        margin-left: 20px;
        text-align: center;
        cursor: pointer;
        &.loginbtn {
          background-color: #0a328e;
          width: 124px;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
        .avatar {
          width: 23px;
          height: 23px;
          border-radius: 50%;
          vertical-align: middle;
        }
      }
    }
  }
}
</style>